package com.curvebreakers.wheresmystuff.tests.model;

import org.json.JSONException;
import org.json.JSONObject;

import com.curvebreakers.wheresmystuff.model.User;

import java.util.Random;

import android.telephony.PhoneNumberUtils;
import android.test.AndroidTestCase;


/**
 * A test case that test the parse user from json functionality in User.
 * @author evan.cahill
 */
public class UserTest extends AndroidTestCase {


	public void testCreateFromJSON() {
		Random rand = new Random();
		
		int id = rand.nextInt();
		String email = "email@thisisnotajoke.com";
		String phoneNum = PhoneNumberUtils.formatNumber("6783679417");
		boolean admin = rand.nextBoolean();
		boolean locked = rand.nextBoolean();
		
		User user = new User(id, email, phoneNum, admin, locked);
		JSONObject json = null;
		try {
			json = new JSONObject()
							.put("id", id)
							.put("email", email)
							.put("phone_number", phoneNum)
							.put("admin", admin ? "t" : "f")
							.put("locked", locked ? "t" : "f");
		} catch (JSONException e) {
			fail("Failed to create the JSONObject for some reason.");
		}
		
		User jsonUser = User.createFromJSON(json);
		
		assertEquals(user.getId(), jsonUser.getId());
		assertEquals(user.getEmail(), jsonUser.getEmail());
		assertEquals(user.getPhoneNum(), jsonUser.getPhoneNum());
		assertEquals(user.isAdmin(), jsonUser.isAdmin());
		assertEquals(user.isLocked(), jsonUser.isLocked());
		
	}

}
